#include "ResourceManager.h"

#include <cstring>

// Framework.
#include "ObjectVisitor.h"
#include "VisualResource.h"

// CoreLib.
#include "LeAssert.h"

le::ResourceManager::ResourceManager()
{

}

le::ResourceManager::~ResourceManager()
{

}

bool le::ResourceManager::LoadFolder( const le::Path& resourceFolder )
{
	// TODO: This is not supported at the moment
	return false;
}

bool le::ResourceManager::LoadFile( const le::Path& resourcePath )
{
	ObjectVisitor visitor;
	if(visitor.Load(resourcePath))
	{
		const int32 nodeCount = visitor.GetCount("Resource");
		for (int32 i = 0; i < nodeCount; ++i)
		{
			if(visitor.EnterNode("Resource"))
			{
				le::AbstractResource* abstractResource = LoadResource(visitor);
				if (abstractResource)
				{
					m_Resources.push_back(abstractResource);
					visitor.ExitNode();
					continue;
				}

				return false;
			}
		}
	}
	return false;
}

le::AbstractResource* le::ResourceManager::LoadResource( le::ObjectVisitor& visitor )
{
	std::string resourceType;
	std::string guid;
	visitor.Visit("ResourceType", resourceType, "");
	visitor.Visit("GUID", guid, "");
	EResourceType resType = GetResourceTypeFromString(resourceType);

	AbstractResource* abstractResource = 0;
	switch(resType)
	{
	case eResourceType_Visual:
		abstractResource = new le::VisualResource("guid");
	default:
		LE_ASSERT(false, "Unkown resource type %s with GUID: %s. Please check for file corruptio.", resourceType.c_str(), guid.c_str());
		return abstractResource;
	}

	abstractResource->Visit(visitor);
	return abstractResource;
}

le::EResourceType le::ResourceManager::GetResourceTypeFromString( const std::string& resourceType ) const
{
	if (std::strcmp("VisualResource", resourceType.c_str()) == 0)
	{
		return eResourceType_Visual;
	}
	return eResourceType_Unkown;
}

std::string le::ResourceManager::GetStringFromResourceType( const EResourceType resourceType ) const
{
	switch (resourceType)
	{
	case eResourceType_Visual:
		return "VisualResource";
	case eResourceType_Unkown:
		return "UnkownResource";
	default:
		return "UnkownResource";
	}
}
